import * as echarts from 'echarts';
import { useEffect, useRef } from 'react';


const BarChart = ({ title }) => {
    const chartRef = useRef(null)
    // 保证dom可用 才进行图表的渲染
    useEffect(() => {
        // 1.获取渲染图标的dom节点 用useRef获取dom
        const chartDom = chartRef.current
        // 2.图表初始化生成图表实例对象
        const myChart = echarts.init(chartDom);
        // 3.准备图表参数
        const option = {
            title: {
                text: title
            },
            xAxis: {
                type: 'category',
                data: ['vue', 'react', 'Angular']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    data: [70, 110, 130],
                    type: 'bar'
                }
            ]
        };
        // 4.使用图标参数完成图表的渲染
        option && myChart.setOption(option);
    }, [])
    return <div><div ref={chartRef} style={{ width: '500px', height: '400px' }}></div></div>
}
export default BarChart